package com.ming.attendance.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.auth0.jwt.JWT;
import com.ming.attendance.mapper.Authorization;
import com.ming.attendance.model.UserInfoModel;
import com.ming.attendance.model.WxUserTokenModel;
import com.ming.attendance.po.ResponseBean;
import com.ming.attendance.po.WxUser;
import com.ming.attendance.service.WxUserService;
import com.ming.attendance.utils.DecryptUtil;
import com.ming.attendance.utils.HttpClientUtil;
import com.ming.attendance.utils.JWTUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/WxUser")
public class WxUserController {

    @Autowired
    private WxUserService wus;

    @Autowired
    private Authorization author;

    // 记得给wx_user字段增加创建账户时间、等字段
    // 给event_user表增加 用户加入事件的时间和打卡成功的时间字段
    @GetMapping("/login")
    public ResponseBean<WxUserTokenModel> login(String code){

        // 获取openid和session_key
        JSONObject jsonObject = code2session(code);
        String session_key = jsonObject.getString("session_key");
        String openid = jsonObject.getString("openid");

        // 包装参数
        WxUser user1 = new WxUser();
        user1.setSessionKey(session_key);
        user1.setOpenid(openid);
        ResponseBean<WxUserTokenModel> result = wus.login(user1);

        return result;
    }

    // 更新用户信息，包括昵称、头像
    @PostMapping("/updateUser")
    public ResponseBean<Integer> updateUser(@RequestHeader("Authorization") String token,WxUser user){
        String openid = JWTUtils.getOpenid(token);
        user.setOpenid(openid);
        user.setLastTime(String.valueOf(new Date().getTime()));
        return wus.updateUser(user);
    }


    // 从微信服务器得到用户的openid和session_key
    private JSONObject code2session(String code){
        String url = "https://api.weixin.qq.com/sns/jscode2session";

        Map paramMap = new HashMap<String,String>();
        paramMap.put("appid",author.getAppId());
        paramMap.put("secret",author.getSecret());
        paramMap.put("js_code",code);
        paramMap.put("grant_type","authorization_code");

        String result = HttpClientUtil.doGet(url, paramMap);

        return JSON.parseObject(result);
    }


    @PostMapping("/getDecryptedData")
    public ResponseBean<Object> getDecryptedData(@RequestHeader("Authorization") String token,String encryptedData,String iv){
        String sessionKey = JWTUtils.getSessionKey(token);
        return wus.getDecryptedData(sessionKey,encryptedData,iv);
    }


    @GetMapping("/getUserInfo")
    public ResponseBean<UserInfoModel> getUserInfo(@RequestHeader("Authorization") String token){
        String openid = JWTUtils.getOpenid(token);
        ResponseBean<UserInfoModel> result = wus.getUserInfo(openid);
        return result;
    }
}
